package bb.lanxing.util.ui;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.vectordrawable.graphics.drawable.ArgbEvaluator;

import bb.lanxing.util.Log;

public class AnimationUtil {
    public static void slideUpShow(final View view, boolean z, final Animator.AnimatorListener animatorListener) {
        if (view.getVisibility() == View.VISIBLE) {
            return;
        }
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationY", z ? measureHeight(view) : view.getHeight(), 0.0f);
        ofFloat.setDuration(200L);
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationStart(animator);
                }
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationEnd(animator);
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationCancel(animator);
                }
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationRepeat(animator);
                }
            }
        });
        ofFloat.start();
    }

    public static void slideDownHide(final View view, final Animator.AnimatorListener animatorListener) {
        if (view.getVisibility() != View.VISIBLE) {
            return;
        }
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationY", 0.0f, view.getHeight());
        ofFloat.setDuration(200L);
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationStart(animator);
                }
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationEnd(animator);
                }
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationCancel(animator);
                }
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                Animator.AnimatorListener animatorListener2 = animatorListener;
                if (animatorListener2 != null) {
                    animatorListener2.onAnimationRepeat(animator);
                }
            }
        });
        ofFloat.start();
    }

    public static void fadeIn(View view, Animator.AnimatorListener animatorListener) {
        ViewPropertyAnimator interpolator = view.animate().alpha(1.0f).setDuration(220L).setInterpolator(new AccelerateDecelerateInterpolator());
        if (animatorListener != null) {
            interpolator.setListener(animatorListener);
        }
        interpolator.start();
    }

    public static void fadeOut(View view, Animator.AnimatorListener animatorListener) {
        ViewPropertyAnimator interpolator = view.animate().alpha(0.0f).setDuration(220L).setInterpolator(new AccelerateDecelerateInterpolator());
        if (animatorListener != null) {
            interpolator.setListener(animatorListener);
        }
        interpolator.start();
    }

    public static Animator animateBackgourdColor(int i, int i2, final View view) {
        ValueAnimator ofInt = ValueAnimator.ofInt(i, i2);
        ofInt.setEvaluator(ArgbEvaluator.getInstance());
        ofInt.setDuration(300L);
        ofInt.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                view.setBackgroundColor((Integer) valueAnimator.getAnimatedValue());
            }
        });
        ofInt.start();
        return ofInt;
    }

    private static int measureHeight(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        int measuredHeight = view.getMeasuredHeight();
        Log.d("measureHeight, measuredHeight = " + measuredHeight);
        return measuredHeight;
    }
}